﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HRMSystemShi.DAL;
using HRMSystemShi.Model;

namespace HRMSystemShi._2025
{
    public partial class FormCreateSalarySheet : Form
    {
        public FormCreateSalarySheet()
        {
            InitializeComponent();
        }
        private void SalarySheet_Load(object sender, EventArgs e)
        {
            comboBoxYear.DataSource = SalarySheetService.GetYearList();
            comboBoxMonth.Enabled = false;
            comboBoxDepartment.DataSource = DepartmentService.GetDepartmentList();
            comboBoxDepartment.DisplayMember = "Name";
            comboBoxDepartment.ValueMember = "Id";
        }

        private void comboBoxYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxMonth.Enabled = true;
            comboBoxMonth.DataSource = SalarySheetService.GetMonthList((int)comboBoxYear.SelectedValue!);
        }

        private void buttonCreateSalarySheet_Click(object sender, EventArgs e)
        {
            int year = (int)comboBoxYear.SelectedValue!;
            int month = (int)comboBoxMonth.SelectedValue!;
            Guid departmentId = (Guid)comboBoxDepartment.SelectedValue!;
            SalarySheet salarySheet = SalarySheetService.GetSalarySheet(year, month, departmentId);
            if (salarySheet != null)
            {
                if (CommonHelper.MessageYesOrNo("该工资单已存在，是否删除并新建工资单（是则删除并新建，否则查看已有工资单）") == DialogResult.Yes)
                {
                    SalarySheetService.DeleteSalarySheetAndSheetItem(salarySheet);
                    salarySheet = new SalarySheet()
                    {
                        Id = Guid.NewGuid(),
                        Year = year,
                        Month = month,
                        DepartmentId = departmentId
                    };
                    SalarySheetService.BuildNewSalarySheetAndItem(salarySheet);
                    dataGridViewSalarySheet.DataSource = SalarySheetService.GetSalarySheetItem(salarySheet);
                }
                else
                {
                    dataGridViewSalarySheet.DataSource = SalarySheetService.GetSalarySheetItem(salarySheet);
                }
            }
            else
            {
                salarySheet = new SalarySheet()
                {
                    Id = Guid.NewGuid(),
                    Year = year,
                    Month = month,
                    DepartmentId = departmentId
                };
                SalarySheetService.BuildNewSalarySheetAndItem(salarySheet);
                dataGridViewSalarySheet.DataSource = SalarySheetService.GetSalarySheetItem(salarySheet);
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridViewSalarySheet.Rows.Count - 1; i++)
            {
                SalarySheetItem item = new SalarySheetItem();
                item.Id = (Guid)dataGridViewSalarySheet.Rows[i].Cells[0].Value;
                item.BaseSalary = (decimal)dataGridViewSalarySheet.Rows[i].Cells[3].Value;
                item.Bonus = (decimal)dataGridViewSalarySheet.Rows[i].Cells[4].Value;
                item.Fine = (decimal)dataGridViewSalarySheet.Rows[i].Cells[5].Value;
                item.Other = (decimal)dataGridViewSalarySheet.Rows[i].Cells[6].Value;
                SalarySheetService.SaveSheetItem(item);
            }
            CommonHelper.MessageSuccess("工资单保存成功!");
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if(CommonHelper.MessageYesOrNo("确定取消吗?") == DialogResult.Yes)
            {
                this.Close();
            }
            else
            {
                CommonHelper.MessageError("取消操作");
            }
        }
    }
}
